package controllers;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import models.Batch;
import models.Commodity;
import models.Order;
import models.BaseEntity.PeopleType;
import models.Order.OrderStatus;
import models.OrderItem;
import models.QueryBean;
import models.RemittanceConfirm;
import models.ShoppingCar;
import models.User;
import models.UserGroup;
import play.data.validation.MinSize;
import play.data.validation.Required;
import play.data.validation.Valid;
import play.mvc.Controller;
import play.mvc.With;
import service.CommoditiesService;
import service.OrderService;

public class Register extends Controller {
	
	/**
	 * 注册页面。
	 */
	public static void register(){
		render();
	}
    
	/**
	 * 提交注册信息
	 * 
	 * @param user
	 */
	public static void commitRegisterInfo(@Valid User user){
		//验证会员输入
		validation.minSize("user.name", 6);
		validation.minSize("user.password", 6);
		if(validation.hasErrors()) {
			render("@register", user);
		}
		
		//设置会员级别
		UserGroup group = UserGroup.find("byName", "普通会员").first();
		List<UserGroup> groups = new ArrayList<UserGroup>();
		groups.add(group);
		
		user.groups = groups;
		user.createAt = new Date();
		user.creatorType = PeopleType.USER;
		user.createByUser = user;
		
		//插入用户数据
		user.save();
		
		//进入管理主页面
		render("@index", user,group);
	}
}